from django.shortcuts import render
from django.views.decorators.http import *
from django.conf import settings
from kkoffer.user.models import job,company,deliver,tag,message,member
from kkoffer.user.models import education as educationObj 
from kkoffer.user.models import work as workObj
from django.core.cache import cache
import re
from django.http import HttpResponseRedirect
from kkoffer.tool.views import login_required
from django.http import HttpResponse
from django.utils.translation import ugettext as _
from pprint import pprint
from django.core.paginator import Paginator


COMPANYTAG = {
    'company':{
            'IPO':_('IPO'),
            'foreign-capital enterprise':_('foreign-capital enterprise'),
            'Female staff above':_('Female staff above'),
            'clothes freedom':_('clothes freedom'),
            'employee stock ownership':_('employee stock ownership'),
            'flex':_('flex'),
            'career guide':_('career guide'),
            'active club':_('active club'),
        },
        'welfare':{
            'pay carfare':_('pay carfare'),
            'pay night shift':_('pay night shift'),
            'pay rent':_('pay rent'),
            'child birth':_('child birth'),
            'The wedding funeral subsidy':_('The wedding funeral subsidy'),
            'qualification reward':_('qualification reward')
        },
        'insurance':{
            'unemployment insurance':_('unemployment insurance'),
            'casualty insurance' : _('casualty insurance'),
            'accident insurance on a business':_('accident insurance on a business'),
            'Special consolation':_('Special consolation'),
            'physical checkup':_('physical checkup'),
            'pension':_('pension'),
            'health insurance':_('health insurance'),
            'team life insurance':_('team life insurance')
        },
        'vacation':{
            'combined holidays above 5 days':_('combined holidays above 5 days'),
            'summer vacation':_('summer vacation'),
            'The wedding funeral vacation':_('The wedding funeral vacation'),
            'Early end of the year':_('Early end of the year'),
            'Paid leave 10 days ~20 days':_('Paid leave 10 days ~20 days'),
            'maternity leave':_('maternity leave'),
            'Once a year take 120 days holiday':_('Once a year take 120 days holiday'),
            }
        }
@require_http_methods(["GET"])
def index(request,word=''):
    context = settings.CLASSIFY
    obj = job.objects
    if word:
        context['list'] = obj.filter(jpName__contains=word,status=1).select_related('uid')
    else:
        context['list'] = job.objects.filter(status=1).select_related('uid')
    paginator = Paginator(context['list'], 10)
    context['rate'] = cache.get('rate') * 10000
    context['cc'] = "tokyo"
    # import ipdb; ipdb.set_trace()
    return render(request,'index.html',context)

def jobs(request,id):
    context = {}
    context['page'] = job.objects.get(id=id)
    context['company'] = context['page'].uid

    tmp = tag.objects.filter(reid=context['page'].uid.pk).values_list('tag')
    jobTmp = tag.objects.filter(reid=context['page'].pk).values_list('tag')   
    context['tag'] = [x[0] for x in tmp]
    context['jobTag'] = [x[0] for x in jobTmp]
    context['tagList'] = COMPANYTAG
    context['rate'] = cache.get('rate') * 10000
    if request.session.get('id'):
        if deliver.objects.filter(comefrom_id=request.session['id'],job_id=id):
            context['text'] = _('Had Send resume')
    return render(request,'jobs.html',context)

def companyJob(request,id):
    context = {}
    context['page'] = company.objects.get(pk=id)
    context['list'] = job.objects.filter(uid = id,status = True)
    context['rate'] = cache.get('rate') * 10000
    return render(request,'companyJob.html',context)

@require_http_methods(["GET"])
def companyList(request,arg):
    context = {}
    context['type'] = company._meta.get_field('industryType').choices
    context['list'] = company.objects.filter(status=True)
    return render(request,'company.html',context)

@require_http_methods(["GET"])
def companyDetail(request,id):
    context = {}
    tmp = tag.objects.filter(reid=id).values_list('tag')
    # import ipdb; ipdb.set_trace()
    context['tagList'] = COMPANYTAG
    context['tag'] = [x[0] for x in tmp]
    context['page'] = company.objects.get(pk = id)
    return render(request,'companyDetail.html',context)

# @login_required
@require_http_methods(["GET"])
def send(request,id):
    uid = request.session['id']
    # import ipdb; ipdb.set_trace()
    try:
        memberData = member.objects.get(pk=uid)
    except Exception:
        return HttpResponse("请完善简历")
    
    workData = workObj.objects.filter(uid=uid).values()
    educationData = educationObj.objects.filter(uid=uid).values()
    if not workData or not educationData or not memberData:
        return HttpResponse("请完善简历")
    # import ipdb; ipdb.set_trace()

    obj = job.objects.get(pk=id)
    deliver.objects.create(comefrom_id=uid,status=1,job_id=id,to_id=obj.uid_id)
    text = memberData.realName + ' 投递' + obj.jpName
    message.objects.create(content = text,comeFrom_id = uid,to_id = obj.uid_id,type = 2)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))

# @login_required
def messageList(request,type):
    context = {}
    id = request.session['id']
    type = type or 'deliver'
    if type == 'deliver':
        context['list'] = message.objects.filter(to_id = id,type = 2).values()
    elif type == 'official':
        context['list'] = message.objects.filter(to_id=0).values()
    # else:
        # context['list'] = message.objects.filter(to_id=0).filter(to_id=id).values()
    # import ipdb; ipdb.set_trace()
    return render(request,'message.html',context)


